$(function() {
	$('.blink').focus(function() {
        if(this.title==this.value) {
            this.value = '';
        }
    }).blur(function(){
        if(this.value=='') {
            this.value = this.title;
        }
    });

    // Navigation drop-down toggle
    $('#main-nav li').hover(function() {
    	if ($('ul', this).length) {
    		$(this).toggleClass('hover');
    		$('ul:eq(0)', this).stop(true,true).slideToggle(200);	
    	};
    });

    // Main Slider  - Fading Effect
    if ($('.slider-1, .slider-3').length > 0) {
        var fader;
        $('#main-slider .item').simpleFade({
            init : function () {
                fader = this;
            },
            onFade : function (index) {
                $('#main-slider .nav a').removeClass('active').eq(index).addClass('active');
            }
        });

        $('.slider-1').mouseover(function(){
            fader.stop();    
        }).mouseleave(function(){
            fader.start();    
        });

        
        $('#main-slider .prev').click(function () {
            fader.prev();
            return false;
        });
        
        $('#main-slider .next').click(function () {
            fader.next();
            return false;
        });
        
        $('#main-slider .nav a').click(function () {
            fader.fadeTo($('#main-slider .nav a').index(this));
            return false;
        });


        $('#main-slider .nav a').mouseenter(function(){
            var posX = (parseInt($(this).position().left) - 59),
                imgSrc = $('img', this).attr('src'),
                tooltip = $('#main-slider .tooltip');

            $('img', tooltip).attr('src', imgSrc);
            $(tooltip).css({
                left: posX
            });
            $(tooltip).show();

        }).mouseleave(function(){
            $('#main-slider .tooltip').hide();
        });

        $('#main-slider .nav').mouseleave(function(){
           $('#main-slider .tooltip').hide(); 
        });
    };
   

    if ($('.slider-2').length > 0) {
        // Main Slider  - Sliding Effect
        $('.slider-2 .container ul').jcarousel({
            visible: 1,
            scroll: 1,
            wrap: "both",
            initCallback: function(carousel){
                $('.slider-2 .nav ul a').click(function(){
                    carousel.scroll( $('.slider-2 .nav ul a').index(this)+1);
                    return false; 
                });
            }
        });

        $('.slider-2 .nav ul').jcarousel({
            visible: 5,
            scroll: 1,
            wrap: "both",
            buttonNextHTML:null,
            buttonPrevHTML:null,
            initCallback: function(carousel){
                $('.slider-2 .nav .prev').click(function(){
                    carousel.prev();
                    return false;
                });

                 $('.slider-2 .nav .next').click(function(){
                    carousel.next();
                    return false;
                });
            }
        });
    };
    

    // Insert the closing quote in testimonials
    $('<span class="quote-bottom"><span>').appendTo('.testimonials p.quote');

    if ( $('.mini-slider ul').length > 0) {
        // Thumbs Carousel
        $('.home-content .mini-slider ul').jcarousel({
            visible: 3,
            scroll: 1,
            wrap: "both",
             buttonNextHTML: null,
            buttonPrevHTML: null,
            itemFirstInCallback : function(carousel, item, idx, state){
                $('.home-content .mini-slider .prev').click(function(){
                    carousel.prev();
                    return false;
                });
                 $('.home-content .mini-slider .next').click(function(){
                    carousel.next();
                    return false;
                });
            }
        });

        $('.portfolio-slider .mini-slider ul').jcarousel({
            visible: 4,
            scroll: 1,
            wrap: "both",
            buttonNextHTML: null,
            buttonPrevHTML: null,
            itemFirstInCallback : function(carousel, item, idx, state){
                $('.portfolio-slider .mini-slider .prev').click(function(){
                    carousel.prev();
                    return false;
                });
                 $('.portfolio-slider .mini-slider .next').click(function(){
                    carousel.next();
                    return false;
                });
            }
        });
    };


    $('#main-nav').mouseenter(function(){
       $('#content').css('z-index', 1); 
    }).mouseleave(function(){
        $('#content').css('z-index', 4); 
    });


    // Call the login
    $('.popup-trigger').click(function(){
        var target = $(this).attr('href');

        $('#overlay').fadeIn(300);
        $(target).fadeIn(500);
        centerPopup();

       return false; 
    });

    $('.popup .close').live('click', function(){
        removePopup();
        return false;  
    });

    // Custom Checkbox 
    $('input[type="checkbox"]').prettyCheckboxes({
        checkboxWidth: 16,
        checkboxHeight: 17,
        display: 'list'
    });

    // Clicking on the overlay while a popup is visible will close both of them
    $(document).click(function(e) {
        if ($(e.target).parents('.popup').length == 0) {
           removePopup();
        }
    });

    // Insert Hover Overlay
    $('<span class="hover"></span>').prependTo('.list-gallery li a');

    // Gallery hover
    $('.list-gallery li').hover(function(){
        $('.hover', this).fadeToggle('fast');
    });

    // Contact focus field
    $('.field').focus(function(){ 
        $(this).addClass('field-focus'); 
    }).blur(function(){ 
        $(this).removeClass('field-focus'); 
    });


    sectionTags();


    $('.tags li').hover(function(){
       $(this).toggleClass('hover');
    });
    
    // Fancybox
    $('a.fancybox').fancybox();

});

function centerPopup(){
    if ($('.popup').length > 0 ) {
        var popup = $('.popup'),
            pHeight = parseInt(popup.height() /2) * -1,
            pWidth = parseInt(popup.width() /2) * -1;
        
        $(popup).css({
            "margin-top":  pHeight,
            "margin-left": pWidth
        });
    }
}

function removePopup(){
    
        $('.popup:visible').fadeOut(function(){
            $('#overlay').fadeOut();
        });
}

function sectionTags(){    
    var li = $('.tags li'),
        zIndex = li.length;

    // Set descreasing z-index to all li's
    $(li).each(function() {
       $(this).css('z-index', zIndex);
       zIndex--;
    });

    // Get first element based on the left position
    $(li).each(function(){
        var liX = $(this).position().left;
        var firstSpan = $('<span />');
        if (liX == 0) {
            $(this).addClass('first');
            firstSpan.prependTo(this);
        };
    });

}

